package ec.atreasure.com.core.fragments.bottom;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.joanzapata.iconify.widget.IconTextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.ColorInt;
import ec.atreasure.com.core.R;
import ec.atreasure.com.core.fragments.AtreasureFragment;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * Description :首页底部导航栏业务控制基类
 * Created by wangjin on 2019/7/6.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public abstract class BaseBottomFragment extends AtreasureFragment implements View.OnClickListener {

    //tab缓存
    private final ArrayList<BottomTabBean> mTabBeans = new ArrayList<>();

    //fragment缓存
    private final ArrayList<BottomItemFragment> mBottomItemFragments = new ArrayList<>();

    //tab和fragment映射缓存
    private final Map<BottomTabBean, BottomItemFragment> mItems = new LinkedHashMap<>();

    //当前选中的tab
    private int mCurrentFragment = 0;

    //首次默认选中的tab
    private int mIndexFragment = 0;

    //tab选中后的颜色
    private int mItemSelectColor = Color.RED;

    //底部导航栏布局
    private LinearLayoutCompat mBottomBar = null;


    @Override
    public Object setLayout() {
        return R.layout.fragment_bottom;
    }

    public ArrayList<BottomItemFragment> getItemFragments() {
        return this.mBottomItemFragments;
    }

    public abstract LinkedHashMap<BottomTabBean, BottomItemFragment> setItems(BottomItemBuilder bottomItemBuilder);

    public abstract int setIndexFragment();

    @ColorInt
    public abstract int setSelectColor();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIndexFragment = setIndexFragment();
        if (setSelectColor() != 0) {
            mItemSelectColor = setSelectColor();
        }

        final BottomItemBuilder builder = BottomItemBuilder.builder();
        final LinkedHashMap<BottomTabBean, BottomItemFragment> items = setItems(builder);
        this.mItems.putAll(items);
        for (Map.Entry<BottomTabBean, BottomItemFragment> entry : this.mItems.entrySet()) {
            final BottomTabBean bottomTabBean = entry.getKey();
            final BottomItemFragment bottomItemFragment = entry.getValue();
            mTabBeans.add(bottomTabBean);
            mBottomItemFragments.add(bottomItemFragment);
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mBottomBar = findViewById(R.id.bottom_bar);
        final int size = this.mItems.size();
        for (int i = 0; i < size; i++) {
            //填充底部导航
            LayoutInflater.from(getContext()).inflate(R.layout.bottom_item_icon_text_layout, mBottomBar);
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);

            //设置每个item点击事件
            item.setTag(i);
            item.setOnClickListener(this);

            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
            final BottomTabBean bean = mTabBeans.get(i);

            //初始化底部数据
            itemIcon.setText(bean.getIcon());
            itemTitle.setText(bean.getTitle());

            if (i == mIndexFragment) {
                itemIcon.setTextColor(mItemSelectColor);
                itemTitle.setTextColor(mItemSelectColor);
            }
        }

        final ISupportFragment[] fragments = this.mBottomItemFragments.toArray(new ISupportFragment[size]);
        getSupportDelegate().loadMultipleRootFragment(R.id.bottom_bar_delegate_container, mIndexFragment, fragments);
    }

    @Override
    public void onClick(View v) {
        final int tabIndex = (int) v.getTag();
        if (tabIndex != mCurrentFragment) {
            changColor(tabIndex);
            getSupportDelegate().showHideFragment(mBottomItemFragments.get(tabIndex), mBottomItemFragments.get(mCurrentFragment));
            mCurrentFragment = tabIndex;
        }
    }

    /**
     * 改变选中tab的颜色
     *
     * @param tabIndex
     */
    public void changColor(int tabIndex) {
        resetColor();
        final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(tabIndex);
        final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
        final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);

        itemIcon.setTextColor(mItemSelectColor);
        itemTitle.setTextColor(mItemSelectColor);
    }

    private void resetColor() {
        final int count = mBottomBar.getChildCount();
        for (int i = 0; i < count; i++) {
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
            itemIcon.setTextColor(Color.GRAY);
            itemTitle.setTextColor(Color.GRAY);
        }
    }
}
